package com.test.mess;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.java.emoji.EmojiConverter;
import com.test.mess.request.EmojiRequest;
import com.test.mess.response.EmojiResponse;
import com.vdurmont.emoji.EmojiParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Pattern;

@Controller
@SpringBootApplication
@Slf4j
public class DemoApplication {

    private EmojiConverter emojiConverter = EmojiConverter.getInstance();


    @GetMapping(value = "/home")
    @ResponseBody
    public String home(@RequestParam String emoji) {
        EmojiConverter emojiConverter = EmojiConverter.getInstance();
        String html = emojiConverter.toHtml(emoji.trim());
        log.info("html={}", html);
        String unicode = emojiConverter.toUnicode(html);
        log.info("unicode={}", unicode);
        return "你好，Spring Boot:" + unicode;
    }

    @PostMapping(value = "/p")
    @ResponseBody
    public EmojiResponse post(@RequestBody EmojiRequest request){
        String emoji = request.getEmoji();
        log.info("emoji={}", JSON.toJSONString(request));
//        Matcher matcher = pattern.matcher(request.getEmoji());
//        log.info("是否包含表情符：{}", matcher.matches());
        log.info("EmojiParser.removeAllEmojis={}", EmojiParser.removeAllEmojis(request.getEmoji()));
//        log.info(emojiConverter.toAlias(request.getEmoji()));
//        log.info(EmojiTool.encodeUnicode(request.getEmoji(), EmojiEncode.HTML_DECIMAL));
        log.info("EmojiParser.parseToHtmlDecimal={}", EmojiParser.parseToHtmlDecimal(emoji));
        String html = emojiConverter.toHtml(request.getEmoji().trim());
        log.info(EmojiParser.removeAllEmojis(request.getEmoji()));
        log.info("html={}", html);
//        String e2s = EmojiConverterUtil.emojiToString(request.getEmoji());
//        log.info("emoji 2 string: {}", e2s);
//        log.info("string 2 emoji: {}", EmojiConverterUtil.stringToEmoji(e2s));
        String unicode = emojiConverter.toUnicode(html);
        log.info("unicode={}", unicode);
        EmojiResponse response = new EmojiResponse();
        response.setEmoji(unicode);
        response.setText(request.getText());
        return response;
    }

private String emojiRegex = "(?:[\uD83C\uDF00-\uD83D\uDDFF]|[\uD83E\uDD00-\uD83E\uDDFF]|[\uD83D\uDE00-\uD83D\uDE4F]|[\uD83D\uDE80-\uD83D\uDEFF]|[\u2600-\u26FF]\uFE0F?|[\u2700-\u27BF]\uFE0F?|\u24C2\uFE0F?|[\uD83C\uDDE6-\uD83C\uDDFF]{1,2}|[\uD83C\uDD70\uD83C\uDD71\uD83C\uDD7E\uD83C\uDD7F\uD83C\uDD8E\uD83C\uDD91-\uD83C\uDD9A]\uFE0F?|[\u0023\u002A\u0030-\u0039]\uFE0F?\u20E3|[\u2194-\u2199\u21A9-\u21AA]\uFE0F?|[\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55]\uFE0F?|[\u2934\u2935]\uFE0F?|[\u3030\u303D]\uFE0F?|[\u3297\u3299]\uFE0F?|[\uD83C\uDE01\uD83C\uDE02\uD83C\uDE1A\uD83C\uDE2F\uD83C\uDE32-\uD83C\uDE3A\uD83C\uDE50\uD83C\uDE51]\uFE0F?|[\u203C\u2049]\uFE0F?|[\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE]\uFE0F?|[\u00A9\u00AE]\uFE0F?|[\u2122\u2139]\uFE0F?|\uD83C\uDC04\uFE0F?|\uD83C\uDCCF\uFE0F?|[\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA]\uFE0F?)";
    Pattern pattern = Pattern.compile(emojiRegex);



    @Autowired
    private static RetryService retryService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        retryService.retry();
    }

}
